﻿using BLL;
using Model;
using System;
using System.Data;
using Thksoft.Modal.Page;

namespace Thksoft.Module.ParameterSettings
{
    public partial class EditEntrust : FormPage
    {
        private Qs_EntrustBLL entrustBLL = new Qs_EntrustBLL();
        private Sys_UserBLL userBLL = new Sys_UserBLL();

        protected override void SaveClick(object sender, EventArgs e)
        {
            decimal principal;
            if (string.IsNullOrEmpty(labRenYuan.Text))
            {
                MessageBox.Show(this, "委托人不能为空");
                return;
            }
            else
            {
                principal = CurrentUserID;//Convert.ToDecimal(labRenYuan.Text);
            }

            if (String.IsNullOrEmpty(this.ddlist.Text.Trim()))
            {
                MessageBox.Show(this, "被委托人不能为空！！！");
                return;
            }
            if (this.ddlist.Text.Trim() == CurrentUserID.ToString())
            {
                MessageBox.Show(this, "被委托人不能为当前登录人！！！");
                return;
            }

            if (FORM_BEGIN_DATE.Value == "")
            {
                MessageBox.Show(this, "请选择开始时间");
                return;
            }
            if (FORM_OVER_DATE.Value == "")
            {
                MessageBox.Show(this, "请选择结束时间");
                return;
            }

            var entity = new Qs_Entrust();
            entity.N_Entrust_Id = 0;
            entity.N_Principal = principal;
            entity.N_User_Id = Convert.ToDecimal(ddlist.Text);//userid;
            entity.D_Ntrust_start_date = Convert.ToDateTime(FORM_BEGIN_DATE.Value);
            entity.D_ntrust_enddata = Convert.ToDateTime(DateTime.Parse(FORM_OVER_DATE.Value).ToString("yyyy-MM-dd") + " 23:59:59");
            entity.C_Entrust_state = FORM_STATE.SelectedValue;
            entity.N_Entrust_remarks = FORM_REMARK.Value.Trim().ToString();
            entity.Company_Id = CurrentUserCompanyID;//FORM_COMPANY.Value;
            entity.D_ntrust_operating_time = DateTime.Now;

            if (FORM_IFEDIT)
            {
                string sql = "select count(1) from qs_entrust q where q.c_entrust_state=0 and q.n_principal=" + entity.N_Principal + " and q.n_user_id=" + entity.N_User_Id + " and q.N_Entrust_Id!=" + FORM_KEY;
                DataTable dt = new BaseBLL().Query(sql);
                if (new BaseBLL().GetCount(sql) > 0)
                {
                    MessageBox.Show(Page, "该被委托人已被委托！！");
                    return;
                }

                #region 修改

                entity.N_Entrust_Id = Convert.ToDecimal(FORM_KEY);

                if (entrustBLL.Update(entity))
                {
                    MessageBox.Show(this, "修改成功");
                }
                else
                {
                    MessageBox.Show(this, "修改异常");
                }

                #endregion 修改
            }
            else
            {
                string sql = "select * from qs_entrust q where q.c_entrust_state=0 and q.n_principal=" + entity.N_Principal + " and q.n_user_id=" + entity.N_User_Id;
                DataTable dt = new BaseBLL().Query(sql);
                if (dt != null && dt.Rows.Count > 0)
                {
                    MessageBox.Show(Page, "该被委托人已被委托！！");
                    return;
                }
                bool fig = entrustBLL.SelectEntTime(entity.N_Principal.ToString().Trim(), DateTime.Now, entity.N_User_Id.ToString().Trim());
                if (fig)
                {
                    MessageBox.Show(Page, "对不起，您的业务还在委托期间，不能再委托!");
                    return;
                }

                #region 新增

                if (entrustBLL.Insert(entity))
                {
                    MessageBox.Show(this, "新增成功");
                    MessageBox.ShowAndRedirect(this, "新增成功", "/Module/ParameterSettings/EditEntrust.aspx?IF_EDIT=FALSE");//TRUE&ID=" + entity.N_Entrust_Id);
                }
                else
                {
                    MessageBox.Show(this, "新增异常");
                }

                #endregion 新增
            }
        }

        protected override void BindPage()
        {
            labRenYuan.Text = CurrentUserName;
            BindUser(CurrentUserCompanyID.ToString());

            if (FORM_IFEDIT)
            {
                var entity = entrustBLL.Find(FORM_KEY);

                var user = userBLL.Find(entity.N_Principal);
                labRenYuan.Text = user.USER_NAME;
                //BindUser(user.USER_COMPANY.ToString());
                ddlist.Text = entity.N_User_Id.ToString();

                FORM_BEGIN_DATE.Value = entity.D_Ntrust_start_date.ToString("yyyy-MM-dd HH:mm:ss");
                FORM_OVER_DATE.Value = entity.D_ntrust_enddata.ToString("yyyy-MM-dd HH:mm:ss");
                FORM_STATE.SelectedValue = entity.C_Entrust_state;
                FORM_REMARK.Value = entity.N_Entrust_remarks;
                D_NTRUST_OPERATING_TIME.InnerText = entity.D_ntrust_operating_time.ToString("yyyy-MM-dd HH:mm:ss");
            }
            else
            {
                Div.Visible = false;
            }
        }

        public void BindUser(string cmyId)
        {
            var dt = userBLL.GetNameByCmyId(cmyId);
            this.ddlist.DataSource = dt;
            this.ddlist.DataTextField = "user_name";
            this.ddlist.DataValueField = "user_id";
            this.ddlist.DataBind();
        }

        //protected override void UpdateFormSize()
        //{
        //    MessageBox.ResponseScript(this, "FORM_SIZE(800,455);");
        //}
        protected override void UpdateTitle()
        {
            if (FORM_IFEDIT)
            {
                MessageBox.ResponseScript(this, "FORM_UPDATE_TITLE(\"编辑委托设置\");");
            }
            else
            {
                MessageBox.ResponseScript(this, "FORM_UPDATE_TITLE(\"添加委托设置\");");
            }
        }
    }
}